import Vue from "vue"
import Vuex from "vuex"
Vue.use(Vuex)
import { unitPageList } from "@/common/api/job/bill.js"
//获取用户基本信息
import { getUserInfo } from '@/common/api/table.js'

import {ACCESS_TOKEN} from '@/store/utals.js'

import app from '@/store/app.js'


export default new Vuex.Store({
	modules: {
		app
	},
	state: {
		unitData: {}, //辅计量
		user_Info: [],	// 登录用户信息
		uni_dataSorce: {},	
		Unbers: '456',
	},
	actions: {
		getUser() {
			getUserInfo().then(res => {
				if (res.code == 200) {
					//存储登录用户信息
					uni.setStorageSync('userInfo', res.data);
					
				}
			})
		}
	},
	mutations: {
		UNIT_DATA(state, data) {
			state.unitData = data;
		},
		getobjlist(state) {
			state.Unbers = ACCESS_TOKEN
		}
	},
	getters: {
		getsll(state){
			getUserInfo().then(res => {
				if (res.code == 200) {
					//存储登录用户信息
					uni.setStorageSync('userInfo', res.data);
					state.user_Info = res.data
				}
			})
			return state
		}
	},
	actions: {
		//获取辅助计量数据
		getUnitSubData({
			commit
		}) {
			return new Promise((resolve, reject) => {
				unitPageList({unitGroupId: -1,size: 1000,current:1}).then(res => {
					if (res.code === 200 && res.data != null) {
						let mapvalue = new Map();
						res.data.records.forEach(item => {
							let unitGroupId = item.unitGroupId
							if(unitGroupId && !item.isMainUnit){
								let unitGroupList = mapvalue.get(unitGroupId)
								if(!unitGroupList){
									unitGroupList = [];
									mapvalue.set(unitGroupId,unitGroupList);
								}
								unitGroupList.push(item);
							}
						})
						commit('UNIT_DATA', mapvalue);
						// resolve()
					}
				}).catch(error => {
					reject(error);
				});
			})
		},
		
		
	},
})
